 Option Explicit	
    Const adTypeBinary = 1
    Const adSaveCreateOverWrite = 2
    Const ForWriting = 2
	
	'Globsal Object
	Dim objShell, objWMIService, oHTTP, objFSO	
	
	'Global Variables
	Dim strSERVER, strDIRECTORY, strSourceURL, strTargetPath, strHTA, strLogPath
    
	Set objShell = CreateObject("WScript.Shell")
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
	Set oHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
	Set objFSO = CreateObject("Scripting.FileSystemObject")
  
  	'Set to the name of the server
    strSERVER = "http://SCCM" 'must not end in \
    'Set to the Directory Path
    strDIRECTORY = "/WILL_DOWNLOAD/" 'must begin and end with \
    
    strSourceURL = strSERVER & strDIRECTORY
    
    strTargetPath = objShell.ExpandEnvironmentStrings("%SystemDrive%")
    If Not Right(strTargetPath,1) = "\" Then 'must end in \
    	strTargetPath = strTargetPath & "\"
    End If
    strTargetPath = strTargetPath & "Scripts\"
    
 	strLogPath = objShell.ExpandEnvironmentStrings("%SystemDrive%")
 	If Not Right(strLogPath,1) = "\" Then 'must end in \
    	strLogPath = strLogPath & "\"
    End If
    strLogPath = strLogPath & "MININT\SMSOSD\OSDLOGS\"
    
 	strHTA = ""
 	
DeleteFiles	
ProcessDirectory
LaunchHTA   

Function DeleteFiles
	Dim objLogsDir, colFiles, oFile
	On Error Resume Next
	Set objLogsDir = objFSO.GetFolder(strLogPath)
	Set colFiles = objLogsDir.Files
	For Each oFile In colFiles
			oFile.Delete
	Next
	Set objLogsDir = objFSO.GetFolder("C:MININT\SMSOSD\OSDLOGS\")
	Set colFiles = objLogsDir.Files
	For Each oFile In colFiles
			oFile.Delete
	Next
        
        ' Clean up any existing C:\MININT directory

If oFSO.FolderExists("C:\MININT") then
	On Error Resume Next
	oFSO.DeleteFolder "C:\MININT", true
	On Error Goto 0
End if

        
        
	If Not objFSO.FolderExists(strTargetPath) Then
		objFSO.CreateFolder(strTargetPath)
	End If
	Set objLogsDir = objFSO.GetFolder(strTargetPath)
	Set colFiles = objLogsDir.Files
	For Each oFile In colFiles
			oFile.Delete
	Next
	On Error GoTo 0

End Function 

Function NewURL
	    	strSourceURL=InputBox("Error Connecting to " & strSourceURL & VbCrLf & "Enter Exit to Exit Script" , "Try another URL" , strSourceURL)
	    	ProcessDirectory
End  Function



Function ProcessDirectory()
If strSourceURL = "Exit" Then WScript.Quit(1)
	Dim arrDATA, i, strLine, strToFind
	On Error Resume Next
	Err.Clear
    oHTTP.Open "GET", strSourceURL, False
    oHTTP.Send
    If Err Then 
    	Err.Clear
		NewURL()
	End If

    arrDATA = Split(oHTTP.ResponseText," ", -1, 1)
    For i = 0 To Ubound(arrData)
			strLine = arrData(i)
			If InStr(1,strLine,"HREF=" & Chr(34) & strDIRECTORY,1) Then
				strToFind = "HREF=" & Chr(34) & strDIRECTORY
				strLine = Replace(strLine, strToFind , "",1,-1,1)
				strLine = Left(strLine,InStr(1,strLine,Chr(34),1)-1)
				'GetFile strLine
				SaveWebBinary strLine
			End If
    Next
End Function

Function SaveWebBinary(strFile) 'As Boolean
	Dim strURL, varByteArray, ado
	If InStr(1,strFile,".hta",1) Then
			strHTA = strFile
	End If
	strURL = strSourceURL & strFile
    oHTTP.Open "GET", strURL, False 
    oHTTP.Send 
    varByteArray = oHTTP.ResponseBody 
    Set ado = CreateObject("ADODB.Stream")
    With ado 
 			.Type = adTypeBinary 
			.Open 
			.Write varByteArray 
			.SaveToFile strTargetPath & strFile, adSaveCreateOverWrite 
			.Close 
	End With
End Function

Function LaunchHTA()
	Dim strSysRoot, strSysDrive, strCMD, oExec, objFolder	
        strSysRoot =  objShell.ExpandEnvironmentStrings("%systemroot%")
        strSysDrive = objShell.ExpandEnvironmentStrings("%SystemDrive%")
	'strCMD = strSysRoot & "\system32\mshta.exe " & strSysDrive & strHTA
		If Not objFSO.FolderExists(strSysDrive & "Scripts") Then
			Set objFolder = objFSO.CreateFolder(strSysDrive & "Scripts")
			Set objFolder = Nothing
		End If
        strCMD = strSysRoot & "\system32\cscript.exe " & strSysDrive & "Scripts\UserGUIWiz.vbs"

	Set oExec = objShell.Exec(strCMD)
	Do While oExec.Status = 0
    	WScript.Sleep 1000

	Loop

	WScript.Sleep 100
End Function



